import count from '@dfeidao/atom-nodejs/db/count';
import operate from '@dfeidao/atom-nodejs/db/operate';
import uuid from 'uuid';

interface Message {
	role_type_name: string;
	tablename: string;
	field: string;
	fieldtitle: string;
	search_field: string;
	search_title: string;
	modelid1: string;
	sysid: string;
	productid: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string) {
	const systemid = '12';
	const { exec: e, prepare: p } = count(action_id, session_id, systemid);
	p('pub_role_type', { role_type_name: msg.role_type_name, productid: msg.productid });
	const [_count] = await e();
	if (_count > 0) {
		return 'repert';
	} else {
		const { insert, exec } = operate(action_id, session_id, systemid);
		insert('pub_role_type', { _id: uuid(), role_type_name: msg.role_type_name, tablename: msg.tablename, field: msg.field, fieldtitle: msg.fieldtitle, search_field: msg.search_field, search_title: msg.search_title, modelid: msg.modelid1, systemid: msg.sysid, productid: msg.productid });
		const res = await exec();
		if (res.code === 1) {
			return 'success';
		} else {
			return 'fail';
		}
	}
}
